﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace table_json
{
    public class IniFile
    {
        private string filePath;

        public IniFile(string path)
        {
            this.filePath = path;
            if (!File.Exists(filePath))
            {
                File.Create(filePath).Close(); // 创建文件
            }
        }

        public string ReadValue(string section, string key)
        {
            try
            {
                string[] lines = File.ReadAllLines(filePath);
                bool sectionFound = false;

                foreach (string line in lines)
                {
                    if (sectionFound && line.Contains(key))
                    {
                        return line.Split('=')[1].Trim();
                    }

                    if (line.Contains("[" + section + "]"))
                    {
                        sectionFound = true;
                    }
                }
            }
            catch (Exception ex) { }

            return null; // 如果未找到对应的键值，返回 null 或者抛出异常
        }
        public void WriteValue(string section, string key, string value)
        {
            string[] lines = File.Exists(filePath) ? File.ReadAllLines(filePath) : new string[0];
            bool sectionFound = false;
            bool keyFound = false;

            for (int i = 0; i < lines.Length; i++)
            {
                if (sectionFound && lines[i].Contains("[" + section + "]"))
                {
                    sectionFound = false; // 如果找到了对应的节，但没有找到对应的键，则在节的末尾添加新的键值对
                    if (!keyFound)
                    {
                        Array.Resize(ref lines, lines.Length + 2);
                        Array.Copy(lines, i, lines, i + 2, lines.Length - i - 2);
                        lines[i] = $"{key}={value}";
                        lines[i - 1] = $"[{section}]";
                        keyFound = true;
                    }
                }

                if (lines[i].Contains("[" + section + "]"))
                {
                    sectionFound = true;
                }

                if (sectionFound && lines[i].Contains(key))
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        lines[i] = $"{key}={value}";
                    }
                    else
                    {
                        lines[i] = $"{key}=";
                    }

                    keyFound = true;
                }
            }

            if (!sectionFound)
            {
                Array.Resize(ref lines, lines.Length + 3);
                lines[lines.Length - 3] = $"[{section}]";
                if (!string.IsNullOrEmpty(value))
                {
                    lines[lines.Length - 2] = $"{key}={value}";
                }
                else
                {
                    lines[lines.Length - 2] = $"{key}=";
                }
            }

            File.WriteAllLines(filePath, lines);
        }
    }
}
